//
//  LykLunBoTu.m
//  GiftSay
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "LykLunBoTu.h"
#import "HomeBannerModel.h"

@interface LykLunBoTu ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSMutableArray *bannerData;
@end

@implementation LykLunBoTu

-(instancetype)initWithFrame:(CGRect)frame withBannerData:(NSMutableArray *)bannerData {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatScrollView];
        UIScrollView *scrollView = (UIScrollView *)[self viewWithTag:1001];
        self.bannerData = [[NSMutableArray alloc] initWithArray:bannerData];
        [self.bannerData insertObject:[self.bannerData lastObject] atIndex:0];
        [self.bannerData addObject:self.bannerData[1]];
        
        for (int i = 0; i < self.bannerData.count; i++) {
            UIImageView *image = [[UIImageView alloc] initWithFrame:self.bounds];
            image.userInteractionEnabled = YES;
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = image.bounds;
            button.tag = 100 + i;
            [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchDown];
            [image addSubview:button];
            image.frame = CGRectMake(self.width * i, 0, self.width, self.height);
            [image setImageWithURL:[NSURL URLWithString:[self.bannerData[i] image_url]] placeholder:nil];
            [scrollView addSubview:image];
        }
//        scrollview可偏移范围
        scrollView.contentSize = CGSizeMake(self.width * self.bannerData.count, 0);
        
//        pages
        UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.width / 4, self.height - (self.width / 2) / 9.8, self.width / 2, (self.width / 2) / 9.8)];
        page.numberOfPages = self.bannerData.count - 2;
        page.tag = 1000;
        page.tintColor = [UIColor whiteColor];
        page.backgroundColor = [UIColor clearColor];
        [self addSubview:page];
        
        NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timer) userInfo:nil repeats:YES];
    }
    return self;
}
#pragma mark -- 创建scrollview
-(void)creatScrollView {
//    scrollview初始化
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView.tag = 1001;
    scrollView.delegate = self;
    [self addSubview:scrollView];
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
//    隐藏滚动条
    scrollView.showsVerticalScrollIndicator = FALSE;
    scrollView.showsHorizontalScrollIndicator = FALSE;
    [scrollView setContentOffset:CGPointMake(scrollView.width, 0) animated:YES];
}

-(void)button:(UIButton *)sender {
    NSInteger i = 0;
    i = sender.tag - 100;
    NSLog(@"%@", [self.bannerData[i] ids]);
}

#pragma mark -- 计时器
-(void)timer {
    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:1001];
    [scroll setContentOffset:CGPointMake(scroll.contentOffset.x + self.width, 0) animated:YES];
    UIPageControl *page = (UIPageControl *)[self viewWithTag:1000];
    page.currentPage = scroll.contentOffset.x / self.width;
    if (scroll.contentOffset.x / self.width == self.bannerData.count - 2) {
        page.currentPage = 0;
    } else if (scroll.contentOffset.x / self.width == self.bannerData.count - 1) {
        page.currentPage = 1;
    }
    if (scroll.contentOffset.x == self.frame.size.width * (self.bannerData.count - 1)) {
        [scroll setContentOffset:CGPointMake(self.width , 0)];
    }
}

#pragma mark -- scrollview协议
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    UIPageControl *page = (UIPageControl *)[self viewWithTag:1000];
    page.currentPage = scrollView.contentOffset.x / self.width - 1;
    if (scrollView.contentOffset.x == 0) {
        [scrollView setContentOffset:CGPointMake(self.width * (self.bannerData.count - 2), 0) animated:NO];
        page.currentPage = self.bannerData.count - 3;
    } else if (scrollView.contentOffset.x == self.width * (self.bannerData.count - 1)) {
        [scrollView setContentOffset:CGPointMake(self.width, 0) animated:NO];
        page.currentPage = 0;
    }
}


@end
